package com.neu.view;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.neu.dao.impl.WorkerDao;
import com.neu.pojo.Worker;
import com.neu.service.impl.WorkerService;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.Font;

public class Login extends JFrame {

	private JPanel contentPane;//面板
	private JTextField textField;//文本框
	private JPasswordField passwordField;//密码框

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Login frame = new Login();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Login() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 464, 339);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("账号");
		lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 22));
		lblNewLabel.setBounds(81, 70, 58, 29);
		contentPane.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("密码");
		lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 22));
		lblNewLabel_1.setBounds(81, 138, 58, 29);
		contentPane.add(lblNewLabel_1);
		
		textField = new JTextField();
		textField.setBounds(169, 67, 171, 32);
		contentPane.add(textField);
		textField.setColumns(10);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(169, 135, 171, 32);
		contentPane.add(passwordField);
		
		JButton btnNewButton = new JButton("确定");
		btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 22));
		btnNewButton.setBounds(101, 208, 87, 37);
		contentPane.add(btnNewButton);
		btnNewButton.addActionListener((e)->{
			String number = textField.getText();
			String password = passwordField.getText();
			
			if(number.length() == 0||password.length() == 0) {
				 JOptionPane.showMessageDialog(null, "请输入账号和密码");
			}else {//输入了
				Worker w = new Worker(number, password);
				
				if(WorkerService.getInstance().login(w)) {//登录成功
					String s = WorkerDao.getInstance().search(w).getJob();
					
					if(s.equals("管理员")) {//跳转到管理员界面
						new AdminUI().setVisible(true);
						setVisible(false);
					}else {//跳转到其他界面
						WorkerUI workerUI = new WorkerUI(WorkerDao.getInstance().search(w));
						workerUI.setVisible(true);
						setVisible(false);							
					}
					
				}else {//登录失败
					JOptionPane.showMessageDialog(null, "账号或密码错误");
				}
			}
		});
		
		JButton btnNewButton_1 = new JButton("退出");
		btnNewButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 22));
		btnNewButton_1.setBounds(253, 208, 87, 37);
		contentPane.add(btnNewButton_1);
		btnNewButton_1.addActionListener((e)->{
			System.exit(0);
		});
		
		//窗口居中
		setLocationRelativeTo(null);
	}
}
